﻿using System;
using System.Data;
using ChapterX.BusinessFacade;
using ChapterX.Common;
using ChapterX.Data.BooksDSTableAdapters;

namespace ChapterX.Modules
{
    public partial class BookListModule : ChapterX.Model.DataListModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindBookList();
            }
        }

        protected void BindBookList()
        {
            try
            {
                TeacherBookViewTableAdapter adpater = new TeacherBookViewTableAdapter();
                DataTable bookList = null;

                if (CurrentUserInfo.IsLibrarian)
                {
                    bookList = adpater.GetData();
                }
                else if (CurrentUserInfo.IsTeacher)
                {
                    bookList = adpater.GetBookListByTeacherId(CurrentUserInfo.Username);
                }
                else
                {
                    throw new UnexpectedException();
                }

                BookList.DataSource = bookList;
                BookList.DataBind();
            }
            catch (Exception ex)
            {
                SetErrorMessage("数据绑定失败。");
                Logger.Log(ex);
            }
        }
    }
}